<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改文章')" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-article-edit" th:object="${article}">
            <input name="articleId" th:field="*{articleId}" type="hidden">
            <div class="form-group">
	            <label class="col-sm-3 control-label">所属栏目：</label>
	            <div class="col-sm-8">
	                <div class="input-group">
	                    <input id="treeId" name="categoryId"  type="hidden"   th:value="*{categoryId}"  />
	                    <input class="form-control" type="text" onclick="selectCategoryTree()" id="treeName" readonly="true"   th:value="*{categoryName}">
	                    <span class="input-group-addon" onclick="selectCategoryTree()"><i class="fa fa-search"></i>选择</span>
	                </div>
	            </div>
	        </div>
            
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">标题：</label>
                <div class="col-sm-8">
                    <input name="title" th:field="*{title}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">摘要：</label>
                <div class="col-sm-8">
                    <textarea name="summary" class="form-control">[[*{summary}]]</textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">主图：</label>
                <div class="col-sm-8 upload_img_wrap">
                    <input type="hidden" id="image"  name="image" th:field="*{image}">
                    <img class="img-sm"  th:src="(${#strings.isEmpty(article.image)}) ? @{/img/upload.png} : @{${article.image}}" onclick="selectSingleMaterial()" >
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">内容：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" th:field="*{content}">
                    <div class="summernote" id="content"></div>
                </div>
            </div>
			
			 <div class="form-group">
				<label class="col-sm-3 control-label is-required">作者：</label>
				<div class="col-sm-3">
					<input name="author" th:field="*{author}" class="form-control" type="text" required>
				</div>
				<label class="col-sm-2 control-label is-required">排序：</label>
				<div class="col-sm-3">
				      <input name="sort" th:field="*{sort}" class="form-control" type="number" required>
				</div>
			</div>
			
			<div class="form-group">
				<label class="col-sm-3 control-label">显示状态：</label>
				<div class="col-sm-3">
					<div class="radio-box" th:each="dict : ${@dict.getType('sys_show_hide')}">
						<input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}" th:field="*{status}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
				<label class="col-sm-2 control-label is-required">是否热门：</label>
				<div class="col-sm-3">
				      <div class="radio-box" th:each="dict : ${@dict.getType('shop_yes_no')}">
						<input type="radio" th:id="${dict.dictCode}" name="isHot" th:value="${dict.dictValue}" th:field="*{isHot}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					  </div>
				</div>
			</div>
			
		   <div class="form-group">    
                <label class="col-sm-3 control-label">是否轮播：</label>
                <div class="col-sm-8">
                     <div class="radio-box" th:each="dict : ${@dict.getType('shop_yes_no')}">
						<input type="radio" th:id="${dict.dictCode}" name="isShow" th:value="${dict.dictValue}" th:field="*{isShow}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					  </div>
                </div>
            </div>
			
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "shop/cms/article";
        $("#form-article-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-article-edit').serialize());
            }
        }

        $(function() {
            $('.summernote').each(function(i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    dialogsInBody: true,
                    callbacks: {
                        onChange: function(contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function(files) {
                            var obj = this;
                            var data = new FormData();
                            data.append("file", files[0]);
                            $.ajax({
                                type: "post",
                                url: ctx + "shop/upload",
                                data: data,
                                cache: false,
                                contentType: false,
                                processData: false,
                                dataType: 'json',
                                success: function(result) {
                                    if (result.code == web_status.SUCCESS) {
                                    	console.log(result.url)
                                        $('#' + obj.id).summernote('insertImage', result.url);
                                    } else {
                                        $.modal.alertError(result.msg);
                                    }
                                },
                                error: function(error) {
                                    $.modal.alertWarning("图片上传失败。");
                                }
                            });
                        }
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            })
        });
        
        
        /*栏目树*/
		function selectCategoryTree() {
			var options = {
					title: '所属栏目',
					width: "380",
					url:ctx + "shop/cms/category/selectCategoryTree/"+$("#treeId").val()+"/0",
					callBack: doSubmit
				};
				$.modal.openOptions(options);
		}
        
		function doSubmit(index, layero){
			var body = $.modal.getChildFrame(index);
   			$("#treeId").val(body.find('#treeId').val());
   			$("#treeName").val(body.find('#treeName').val());
   			$.modal.close(index);
		}
		
		/*素材单选框*/
		function selectSingleMaterial() {
			var options = {
				title : '素材库',
				width : "1500",
				url : ctx + "shop/plugin/material/single",
				callBack : doSingleSubmit
			};
			$.modal.openOptions(options);
		}
		
		function doSingleSubmit(index, layero) {
			var url = layero.find("iframe")[0].contentWindow.getSelections();
		    if(url != ""){
		      $("#image").val(url);	
		      $(".img-sm").attr("src",url);	
			  $.modal.close(index);
		    }
			
		}
    </script>
</body>
</html>